package com.wct.demo.gui;


import com.wct.demo.BankTask;
import com.wct.demo.MyMockTask;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WEI (Email:changtan20000@163.com)
 * @description SimpleTableDemo.java requires no other files.
 * @date 2019/12/25 21:46
 */
public class SimpleTableDemo2 extends JPanel {

    private SimpleTableDemo2() {
        super(new GridLayout(1, 0));

        String[] columnNames = {"1",
                "2",
                "3",
                "4",
                "5"};

        int count = 10;


        Object[][] data = new Object[count][];
        for (int i = 0; i < count; i++) {
            data[i] = MyMockTask.mock().toObjectArray();
        }
//        JTable table = new JTable(data, columnNames);

        ArrayList<BankTask> taskList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            taskList.add(MyMockTask.mock());
        }
        MyListTableModel myListTableModel = new MyListTableModel(taskList, columnNames);
        JTable table = new JTable(myListTableModel);

        table.setPreferredScrollableViewportSize(new Dimension(900, 400));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SimpleTableDemo2 newContentPane = new SimpleTableDemo2();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(SimpleTableDemo2::createAndShowGUI);
    }

    private class MyListTableModel extends AbstractTableModel{
        private ArrayList list;
        private final String headers[];

        public MyListTableModel(ArrayList<BankTask> list, String[] headers){
            this.list = list;
            this.headers =headers;
        }

        @Override
        public int getRowCount() {
            return list.size();
        }

        @Override
        public int getColumnCount() {
            return headers.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            BankTask task = (BankTask) list.get(rowIndex);
            return task.toObjectArray()[columnIndex];
        }
    }
}
